package com.tfswue.ershoujiaoyi.controller;

import com.tfswue.ershoujiaoyi.entity.User;
import com.tfswue.ershoujiaoyi.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理", description = "用户注册、登录、信息修改和删除")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "用户注册", description = "新用户注册账号")
    @PostMapping("/register")
    public User register(@RequestBody User user) {
        return userService.register(user);
    }

    @Operation(summary = "用户登录", description = "用户通过用户名和密码登录")
    @PostMapping("/login")
    public User login(
            @Parameter(description = "用户名") @RequestParam String username,
            @Parameter(description = "密码") @RequestParam String password) {
        return userService.login(username, password);
    }

    @Operation(summary = "查询所有用户", description = "获取全部用户列表")
    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    @Operation(summary = "根据ID查询用户", description = "根据用户ID查询用户信息")
    @GetMapping("/{id}")
    public User getById(@Parameter(description = "用户ID", required = true) @PathVariable Long id) {
        return userService.getById(id);
    }

    @Operation(summary = "修改用户信息", description = "根据用户ID修改用户信息")
    @PutMapping("/update")
    public User update(@RequestBody User user) {
        return userService.update(user);
    }

    @Operation(summary = "删除用户", description = "根据用户ID删除用户")
    @DeleteMapping("/delete/{id}")
    public String delete(@Parameter(description = "用户ID", required = true) @PathVariable Long id) {
        boolean success = userService.delete(id);
        return success ? "删除成功" : "删除失败";
    }
}
